package com.hotel.BL;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.travel;

public class TravelLogic extends AbstractBL {
	public travel Gettravel(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Travel, id);
		try {
			Entity lang = datastore.get(key);
			travel c = new travel();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public travel Gettravel(long hotelId,long id)
	{
		Query q = new Query(EntityName.Travel);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterId = new FilterPredicate("id",FilterOperator.EQUAL,id);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterId);
		
		q.setFilter(filter);
		
		return Gettravel(datastore.prepare(q).asSingleEntity());
	}
	
	public List<travel> GetAlltravel(long hotelId,long id)
	{
		Query q = new Query(EntityName.Travel);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public travel Gettravel(Entity entity)
	{
		travel item = new travel();
		item.setEntity(entity);
		return item;
	}
	
	public List<travel> GetList(List<Entity> entities)
	{
		List<travel> list = new ArrayList<travel>();
		for(Entity e : entities)
		{
			travel a = new travel();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public travel Addtravel(long hotelId,String tarvelby,String arrive,Date time ,boolean addTask,String tarvelbyBack,String arriveBack,Date timeBack ,boolean addTaskBack,boolean showTime,boolean showTimeBack) throws EntityNotFoundException{
		travel t = new travel();
		t.setHotelId(hotelId);
		t.setTarvelby(tarvelby);
		t.setArrive(arrive);
		
		t.setTime(time);
		t.setAddTask(addTask);
		t.setTarvelbyBack(tarvelbyBack);
		t.setArriveBack(arriveBack);
		
		t.setTimeBack(timeBack);
		t.setAddTask(addTaskBack);
		t.setShowTime(showTime);
		t.setShowTimeBack(showTimeBack);
		t.setAvaliable(true);
		t.setStatus(true);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public travel Edittravel(long id,String tarvelby,String arrive,Date time ,boolean addTask ,String tarvelbyBack,String arriveBack,Date timeBack ,boolean addTaskBack,boolean showTime,boolean showTimeBack) throws EntityNotFoundException{
		
		travel tt = Gettravel(id);
		
		tt.setTarvelby(tarvelby);
		tt.setArrive(arrive);
	
		tt.setTime(time);
		tt.setAddTask(addTask);
		tt.setTarvelbyBack(tarvelbyBack);
		tt.setArriveBack(arriveBack);
		tt.setShowTime(showTime);
		tt.setShowTimeBack(showTimeBack);
		tt.setTimeBack(timeBack);
		tt.setAddTask(addTaskBack);
		tt.setAvaliable(true);
		tt.setStatus(true);
		Key	k=datastore.put(tt.getEntity());
		Entity e = datastore.get(k);
		
		tt = new travel();
		tt.setEntity(e);

		return tt;
	}
	
	public travel Deletetravel(long id) throws EntityNotFoundException{
		travel t = Gettravel(id);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public travel ChangeStatus(long modearrivalId) throws EntityNotFoundException{
		
		travel a = Gettravel(modearrivalId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
}
